package net.siufung.security.base.exception.enums;

import cn.hutool.core.util.StrUtil;
import lombok.Getter;
import lombok.Setter;
import net.siufung.core.result.IResultType;
import net.siufung.core.result.ResultFail.ResultErrorCode;

/**
 * 权限错误错误对应错误码
 * @author 陈建峰
 * @since 2022/3/3 4:03 下午
 */
public enum Oauth2ErrorCodes implements IResultType {

    /**
     * unauthorized
     */
    UNAUTHORIZED("unauthorized", ResultErrorCode.AUTH_UNAUTHORIZED_CODE),
    /**
     * access_denied
     */
    ACCESS_DENIED("access_denied", ResultErrorCode.AUTH_ACCESS_DENIED_CODE),
    /**
     * invalid_unknown
     */
    INVALID_UNKNOWN("invalid_unknown", ResultErrorCode.AUTH_INVALID_UNKNOWN_CODE),
    /**
     * invalid_captcha
     */
    INVALID_CAPTCHA("invalid_captcha", ResultErrorCode.AUTH_INVALID_CAPTCHA_CODE),
    /**
     * invalid_request
     */
    INVALID_REQUEST("invalid_request", ResultErrorCode.AUTH_INVALID_REQUEST_CODE),
    /**
     * invalid_grant
     */
    INVALID_GRANT("invalid_grant", ResultErrorCode.AUTH_INVALID_GRANT_CODE),
    /**
     * invalid_client
     */
    INVALID_CLIENT("invalid_client", ResultErrorCode.AUTH_INVALID_CLIENT_CODE),
    /**
     * invalid_scope
     */
    INVALID_SCOPE("invalid_scope", ResultErrorCode.AUTH_INVALID_SCOPE_CODE),
    /**
     * invalid_token
     */
    INVALID_TOKEN("invalid_token", ResultErrorCode.AUTH_INVALID_TOKEN_CODE);

    @Getter
    @Setter
    private String message;

    @Getter
    @Setter
    private Integer code;

    Oauth2ErrorCodes(String message, Integer code) {
        this.message = message;
        this.code = code;
    }

    public static Integer getCode(String error) {
        Oauth2ErrorCodes[] authErrorCodes = values();
        for (Oauth2ErrorCodes authErrorCode : authErrorCodes) {
            if (StrUtil.equals(authErrorCode.getMessage(), error)) {
                return authErrorCode.getCode();
            }
        }
        return ResultErrorCode.DEFAULT_ERROR_CODE;
    }

}
